package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.SetmealCheckGroupDao;
import com.itheima.dao.SetmealDao;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.pojo.Setmeal;
import com.itheima.pojo.User;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealDao setmealDao;

    @Autowired
    private SetmealCheckGroupDao setmealCheckGroupDao;


    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();

        //PageHelper 应该是提Maven提供的
        Page<Setmeal> pages = PageHelper.startPage(currentPage, pageSize);

        setmealDao.findAllSetMeal(queryString);

        return new PageResult(pages.getTotal(), pages.getResult());
    }

    /*新增套餐*/
    @Override
    public void addSetmeal(List<Integer> checkgroupIds, Setmeal setmeal) {

        setmealDao.addSetmeal(setmeal);

        Integer setmealId = setmeal.getId();

        for (Integer checkgroupId : checkgroupIds) {

            setmealCheckGroupDao.addCheckGroupIdsBySetmealId(setmealId,checkgroupId);

        }

    }

    @Override
    public void deleteSetmealById(Integer setmealId) {

        setmealCheckGroupDao.deleteCheckGroupIdsBySetmealId(setmealId);

        setmealDao.deleteSetmealById(setmealId);

    }

    @Override
    public Setmeal queryById(Integer id) {

        Setmeal setmeal = setmealDao.queryById(id);

        return setmeal;
    }

    @Override
    public List<Integer> queryCheckGroupIdsBySetmealId(Integer setmealId) {

        List<Integer> CheckGroupIds = setmealDao.queryCheckGroupIdsBySetmealId(setmealId);

        return CheckGroupIds;
    }

    @Override
    public void updateSetmeal(Setmeal setmeal, List<Integer> checkGroupIds) {

        Integer setmealId = setmeal.getId();

        int number = setmealCheckGroupDao.deleteCheckGroupIdsBySetmealId(setmealId);
        System.out.println("受影响的行数====>"+number);

        for (Integer checkGroupId : checkGroupIds) {

            setmealCheckGroupDao.addCheckGroupIdsBySetmealId(setmealId,checkGroupId);

        }

        setmealDao.updateSetmeal(setmeal);

    }


}
